package it.polimi.swimv2.entities;

import it.polimi.swimv2.util.StringCapitalizer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.*;

/**
 * Entity implementation class for Entity: User.
 * Represents an user of swimv2.
 */
@Entity
@Table(name="UserData")
public class User extends AbstractUser {
	private static final long serialVersionUID = 1L;
	
	
	
	@Column(name="firstname",nullable=false)
	private String firstname;
	
	@Column(name="surname",nullable=false)
	private String surname;
	
	@Column(name="email",nullable=false)
	private String email;
	
	@Column(name="city")
	private String city;
	
	@Column(name="phone")
	private String phone;
	
	@Column(name="totScore",nullable=false)
	private float totScore;
	
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(
		    name="DeclaredAbilities",
		    joinColumns=
		        @JoinColumn(name="username", referencedColumnName="username"),
		    inverseJoinColumns=
		        @JoinColumn(name="abilityName", referencedColumnName="name")
		    )
	private List<Ability> declaredAbilities;

	
	/**
	 * Default constructor.
	 */
	public User() {
		super();
		setTotScore(0);
		this.declaredAbilities= new ArrayList<Ability>();
		
	}
	
	/**
	 * 
	 * @param username
	 * @param password
	 * @param firstname
	 * @param surname
	 * @param email
	 * 
	 */
	public User(String username, String password, String firstname,
			String surname, String email) {
		
		super(username,password);
		setFirstname(firstname);
		setSurname(surname);
		setEmail(email);
		setTotScore(0);
		this.declaredAbilities= new ArrayList<Ability>();
	}

	
	/**
	 * @return the firstname
	 */
	public String getFirstname() {
		return firstname;
	}

	/**
	 * @param firstname the firstname to set
	 */
	public void setFirstname(String firstname) {
		this.firstname = StringCapitalizer.capitalize(firstname);
	}

	/**
	 * @return the surname
	 */
	public String getSurname() {
		return surname;
	}

	/**
	 * @param surname the surname to set
	 */
	public void setSurname(String surname) {
		this.surname = StringCapitalizer.capitalize(surname);
	}

	/**
	 * @return the city
	 */
	public String getCity() {
		return city;
	}

	/**
	 * @param city the city to set
	 */
	public void setCity(String city) {
		this.city = StringCapitalizer.capitalize(city);
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}

	/**
	 * @param phone the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}

	/**
	 * @return the totScore
	 */
	public float getTotScore() {
		return totScore;
	}

	/**
	 * @param totScore the totScore to set
	 */
	public void setTotScore(float totScore) {
		this.totScore = totScore;
	}

	/**
	 * 
	 * @return
	 */
	public Collection<Ability> getDeclaredAbilities() {
		return new ArrayList<Ability>(this.declaredAbilities);
	}
	
	/**
	 * 
	 * @param a
	 */
	public void removeAbility(Ability a){
		this.declaredAbilities.remove(a);
	}
	
	/**
	 * 
	 * @param a
	 */
	public void addAbility(Ability a) {
		this.declaredAbilities.add(a);
	}
	
	
	
}

